package ink.tiptop.service;

import ink.tiptop.common.cache.ECaches;
import ink.tiptop.common.cache.ICache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import ink.tiptop.utils.JSON;
/**
 * @author ink.tiptop.zjf
 * @Title: Ticket
 * @Package ink.tiptop.service
 * @Description:
 * @date 2020-07-27 16:16
 */
@Component("cacheSearch")
@Slf4j
public class CacheSearch<T> {

    @Autowired
    private ICache<String> cache;

    public List<T> getList(String key, Class<T> t) {
        return this.getList(key, t, ECaches.DEFAULT);
    }

    public List<T> getList(String key, Class<T> t , ECaches eCaches) {
        String val = cache.getVal(key ,eCaches);
        if (val == null) {
            return null;
        }
        try {
            return JSON.json2list(val, t);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return new ArrayList<>();
        }
    }

}
